package xq.gwt.ui.crud;

import xq.gwt.mvc.model.EntityChangedEvent;
import xq.gwt.mvc.model.EntityChangedListener;
import xq.gwt.mvc.model.PersistableEntityModel;
import xq.gwt.mvc.model.PropertyGroup;
import xq.gwt.ui.widgets.feedback.FeedbackPresenter;
import xq.gwt.ui.widgets.feedback.FeedbackService;

import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;

public class PopupEntityEditor implements FeedbackPresenter, EntityChangedListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2409559778202531699L;
	private PersistableEntityModel entity;
	protected EntityEditorModel editorModel;
	final Window winModal = new Window();
	private FeedbackPresenter feedbackPresenterSave;
	private CrudPermisions permissions;
	private PropertyGroup[] propertyGroups;
	private int editorColumns;	
	private EntityDetailEditor entityEditor;
	
	public PopupEntityEditor(PersistableEntityModel entity){
		this.entity = entity;
		init();
	}
	
	public PopupEntityEditor(PersistableEntityModel entity, CrudPermisions permissions){
		this.entity = entity;
		this.permissions = permissions;
		init();
	}
	
	public PopupEntityEditor(PersistableEntityModel entity, CrudPermisions permissions, PropertyGroup[] propertyGroups, int editorColumns){
		this.entity = entity;
		this.permissions = permissions;
		this.propertyGroups = propertyGroups; 
		this.editorColumns = editorColumns;
		init();
	}
	
	private void init(){
		editorModel = new EntityEditorModel();
		editorModel.setEntity(entity);
		editorModel.setPropertyGroups(propertyGroups);
		
		feedbackPresenterSave = FeedbackService.getFeedbackPresenter();
		FeedbackService.setFeedbackPresenter(this);
		if(permissions != null){
			editorModel.setInsertDenied(permissions.insertDenied);
			editorModel.setUpdateDenied(permissions.updateDenied);
			editorModel.setDeleteDenied(permissions.deleteDenied);
		}
		editorModel.addEntityChangedListener(this);
	}
	
	protected int getWindowHeight(){
		return 700;
	}
	
	protected int getWindowWidth(){
		return 1000;
	}
	
	protected String getTitle(){
		if(entity == null){
			return "";
		}
		return entity.getTitle();
	}
	
	public void show() {		
		winModal.setWidth(getWindowWidth());
		winModal.setHeight(getWindowHeight());
		winModal.setTitle(getTitle());
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setShowModalMask(true);
		winModal.centerInPage();
		winModal.setShowFooter(true);
		winModal.addCloseClickHandler(new CloseClickHandler() {
			@Override
			public void onCloseClick(CloseClientEvent event) {
				close();
			}
		});
		
		 
		buildEntityEditor();
		winModal.addItem(entityEditor.getCanvas());
		winModal.show();
	}
			
	protected void close(){
		FeedbackService.setFeedbackPresenter(feedbackPresenterSave);
		winModal.destroy();
	}
	
	@Override
	public void showFeedback(String feedback) {
		if(this.winModal == null)
			return;
		winModal.setStatus(feedback);		
	}

	@Override
	public void entityChanged(EntityChangedEvent event) {
		if(!winModal.isVisible()){
			return;
		}		
		winModal.removeItem(entityEditor.getCanvas());
		buildEntityEditor();
		winModal.addItem(entityEditor.getCanvas());
		
	}

	private void buildEntityEditor(){
		entityEditor = new EntityDetailEditor();
		if(editorColumns != 0){
			entityEditor.setNumCols(editorColumns);
		}		
		if(propertyGroups != null){
			entityEditor.setPropertyGroups(propertyGroups);
		}
		entityEditor.setEntity(editorModel);
	}
}

